var orm = require('orm');

var express_orm = orm.connect(require("./config/mysql_config.js"), function(err, db) {
    if (err) throw err;
    var models = {};
    // for (var tablename in tables) {
    //     (function(tablename) {
    //         console.log("ORM定义初始化定义表：", tablename);
    //         var table_info = tables[tablename];
    //         for (var key in table_info) {
    //             table_info[key] = typeMap[table_info[key]];
    //         }
    //         models[tablename] = db.define(tablename, table_info);
    //     }(tablename));
    // }
    table_info = {
        "s_id": "int",
        "s_name": "varchar",
        "s_area": "varchar",
        "s_school": "varchar",
        "s_major": "varchar",
        "s_grade": "varchar",
        "s_birthday": "double",
        "s_contact": "varchar",
        "s_remark": "text",
    }
    for (var key in table_info) {
        table_info[key] = typeMap[table_info[key]];
    }
    models.student = db.define("student", table_info, {
        id: "s_id"
    });

    models.student.find({
        s_id: 1
    }, function(err, result) {
        result = result[0]
        console.log(err, result);
        for (var i in result) {
            console.log("\t\t\t", result[i]);
        }
    });
    setTimeout(function() {
        db.close();
    }, 1000);
});

/*
 *  数据库表映射信息
 */
var typeMap = {
    "tinyint": Number,
    "int": Number,
    "double": Number,
    "varchar": String,
    "text": String,
}
var tables = {
    "achievement": {
        "a_id": "int",
        "a_s_id": "varchar",
        "a_c_id": "varchar",
        "a_registration_time": "double",
        "a_learning_time": "double",
        "a_current_price": "varchar",
        "a_accounts_payable": "varchar",
        "a_p_id": "varchar",
        "a_remark": "varchar",
        "a_s_consultants": "int",
        "a_s_marketing_personnel": "int",
        "a_s_teachers": "int",
    },
    "after_service": {
        "as_id": "int",
        "as_r_id": "int",
        "as_servicer": "varchar",
        "as_teacher": "varchar",
        "as_remark": "text",
        "as_effect": "varchar",
        "as_composition_courses": "varchar",
        "as_course_schedule": "varchar",
    },
    "class": {
        "c_id": "int",
        "c_name": "varchar",
        "c_price": "varchar",
        "c_remark": "text",
        "c_type": "varchar",
    },
    "payment": {
        "p_id": "int",
        "p_poor": "tinyint",
        "p_installments": "tinyint",
        "p_status": "varchar",
        "p_earnest": "double",
        "p_credit": "double",
        "p_interest": "double",
        "p_fee": "double",
    },
    "staff": {
        "s_id": "int",
        "s_name": "varchar",
        "s_contact": "varchar",
        "s_remark": "text",
        "s_position": "varchar",
    },
    "student": {
        "s_id": "int",
        "s_name": "varchar",
        "s_area": "varchar",
        "s_school": "varchar",
        "s_major": "varchar",
        "s_grade": "varchar",
        "s_birthday": "double",
        "s_contact": "varchar",
        "s_remark": "text",
    }
};

module.exports = express_orm;
